package com.neusoft.freshmenapp.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.neusoft.freshmenapp.R;
import com.neusoft.freshmenapp.util.HttpUtil;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    //toolbar控件
    private Toolbar toolbar;
    /**
     * 注册按钮
     */
    private Button bt_register;
    /**
     * 用户名
     */
    private EditText et_userName;
    /**
     * 真实姓名
     */
    private EditText et_name;
    /**
     * 邮箱
     */
    private EditText et_email;
    /**
     * 电话
     */
    private EditText et_phone;
    /**
     * 就读学院
     */
    private Spinner sp_college;
    /**
     * 就读专业
     */
    private Spinner sp_major;
    /**
     * 密码
     */
    private EditText et_password;
    /**
     * 重复密码
     */
    private EditText et_repeat_password;
    /**
     * 就读部门
     */
    String college;
    /**
     * 就读专业
     */
    String major;
    private String[] colleges;
    private String[] majors;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //找到toolbar控件
        toolbar = findViewById(R.id.toolbar);
        //初始化toolbar
        setSupportActionBar(toolbar);
        //显示返回按钮
        showBackMenu();

        //找控件
        et_userName = findViewById(R.id.et_userName);
        et_name = findViewById(R.id.et_name);
        et_email = findViewById(R.id.et_email);
        et_phone = findViewById(R.id.et_phone);
        sp_college = findViewById(R.id.sp_college);
        sp_major = findViewById(R.id.sp_major);
        et_password = findViewById(R.id.et_password);
        et_repeat_password = findViewById(R.id.et_repeat_password);
        bt_register = findViewById(R.id.bt_register);
        bt_register.setOnClickListener(this);


        colleges = getResources().getStringArray(R.array.college);
        majors = getResources().getStringArray(R.array.major);


        sp_college.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                college = colleges[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                college = colleges[0];
            }
        });
        sp_major.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                major = majors[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                major = majors[0];
            }
        });

    }

    /**
     * 登录按钮和注册按钮点击回调
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_register:
                String url = "http://10.0.2.2:8080/freshmenapp/user/regist";
                String password = et_password.getText().toString();
                String repeatPassword = et_repeat_password.getText().toString();
                if (password.equals(repeatPassword)) {
                    String name = et_userName.getText().toString();
                    String email = et_email.getText().toString();
                    String telephone = et_phone.getText().toString();
                    String role = "学生";
                    registerAction(url,name,password,email,telephone,college,major,role);

                } else {
                    Toast.makeText(this,"两次密码不一致，请重新输入",Toast.LENGTH_SHORT).show();
                    et_repeat_password.setText(null);
                }
                break;


        }
    }

    private void registerAction(String url, final String name, String password, String email, String telephone, String college, String major, String role) {
     JSONObject json = new JSONObject();
     try {
         json.put("name",name);
         json.put("password",password);
         json.put("email",email);
         json.put("telephone",telephone);
         json.put("department",college);
         json.put("major",major);
         json.put("role","学生");

     }catch (JSONException e){
         e.printStackTrace();
     }

      HttpUtil.request(url, json, new Callback() {
         @Override
         public void onFailure(@NotNull Call call, @NotNull IOException e) {
             e.printStackTrace();
         }

         @Override
         public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
             final String responseData = response.body().string();
             Log.d("register", "onResponse: "+responseData);
             runOnUiThread(new Runnable() {
                 @Override
                 public void run() {
                     try {
                         JSONObject jsonObject = new JSONObject(responseData);
                         String result = jsonObject.getString("result");
                         if ("true".equals(result)) {
                             et_email.setText("");
                             et_name.setText("");
                             et_password.setText("");
                             et_repeat_password.setText("");
                             et_userName.setText("");
                             Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                         }else {
                             Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_LONG).show();
                         }

                     } catch (JSONException e) {
                         e.printStackTrace();
                     }
                 }
             });
         }
     });
    }




    /**
     * 显示返回按钮
     */
    protected void showBackMenu() {
        //显示返回按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    /**
     * 菜单点击了回调
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            //当返回按钮点击了
            case android.R.id.home:
                //关闭当前页面
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}
